<?php
    class Admincp_SpecialreservationController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_locale;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->view->headTitle('Quản lý đặt phòng');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Specialreservation();
            $this->_locale = 'vi';
        }
        
        public function preDispatch()
		{
		    $auth = Zend_Auth::getInstance();
		    if(!$auth->hasIdentity()) {
		        if($this->_request->getActionName() != 'login')
		            $this->_redirect('admincp/index/login');
		    }
		}
        
        public function indexAction()
        {
            if($_POST['refresh']) {
                $this->_redirect('admincp/specialreservation');
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
                $this->_model->deleteMultiItem($listId);
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }
            
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listItem($this->_locale));
            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data = $paginator;
            $this->view->total = $paginator->getTotalItemCount();
        }
        
        public function editAction()
        {
            $this->_helper->layout->setLayout('popup');
            $id = (int) $this->_arrParam['id'];
            $this->view->data = $this->_model->getItem($id);
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->_model->deleteItem($id);
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function publishAction()
        {
            $id = (int) $this->_arrParam['id'];
            $status = (int) $this->_arrParam['status'];
            $status = ($status == 1) ? 2 : 1;
            $data = array(
                'id' => $id,
                'status' => $status,
            );
            $this->_model->saveItem($data, 'update');
            $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function searchAction()
        {
            $strFlex = new Zendvn_Filter_RemoveCircumflex();
		    $hotel = strtolower(trim(strip_tags($this->_arrParam['hotel'])));
            $fullname = strtolower(trim(strip_tags($this->_arrParam['fullname'])));
            $phone = strtolower(trim(strip_tags($this->_arrParam['phone'])));
		    $type = (int) $this->_arrParam['type'];
		    $this->view->keyword = array(
	            'hotel'=>$hotel,
                'fullname'=>$fullname,
                'phone'=>$phone,
	            'type'=>$type,
		    );
		    $data = array(
	            'hotel'=>(string) '%'.str_replace(' ','%',$strFlex->filter($hotel)).'%',
                'fullname'=>(string) '%'.str_replace(' ','%',$strFlex->filter($fullname)).'%',
                'phone'=>(string) '%'.str_replace(' ','%',$strFlex->filter($phone)).'%',
	            'type'=>$type,
		    );
		    $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->search($data));
		    $paginator = new Zend_Paginator($adapter);
		    $paginator->setItemCountPerPage(40);
		    $paginator->setPageRange(5);
		    $currentPage = $this->_request->getParam('page', 1);
		    $paginator->setCurrentPageNumber($currentPage);
		    $this->view->data = $paginator;
		    $this->view->total = $paginator->getTotalItemCount();
        }
    }
